<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/26
 * Time: 15:30
 */

namespace app\service;

use app\models\Baoxiao;
use yii;
use app\models\Bank_bill;
use app\models\CommonModel;
use app\models\Express;
use app\models\Income;
use app\models\Inland;
use app\models\Invoice_new;
use app\models\PrintModel;
use app\models\Repo;

class TableService
{
    /**合并数据
     * @param array $where
     * @return array
     */
    public function mergeOrderData($where = [])
    {
        $model = new Invoice_new();
        $list = $model->getList($where);
        $invoice_arr = [];
        foreach ($list as $v) {
            $invoice_arr[] = $v['invoice'];
        }
        $model = new Income();
        $incomeList = $model->get_group_income_by_invoice($invoice_arr);
        //invoice添加运费
        $express_model = new Express();
        $express_list = $express_model->get_all_express_money($invoice_arr);
        //invoice添加仓库费用
        $repo_model = new Repo();
        $repofee_list = $repo_model->get_repo_fee_by_invoice($invoice_arr);

        //打印费用添加
        $print_model = new PrintModel();
        $print_list = $print_model->get_print_fee_by_invoice($invoice_arr);


        //大货报销支出
        $bank_bill = new Bank_bill();
        $bank_list = $bank_bill->get_money_by_invoice($invoice_arr);

        $othersList = $bank_bill->get_others_by_invoice($invoice_arr);

        //报销费用添加
        $baoxiaoModel = new Baoxiao();
        $baoxiaoList = $baoxiaoModel->getListByParam($invoice_arr);
        //添加汇率
        $commonModel = new CommonModel();
        $exchange_list = json_decode($commonModel->get_all_exchange_rate(), true);
        $exchange_list[] = [
            'month' => date('Ym'),
            'rate' => yii::$app->params['exchange_rate'],
            'account_rate' => yii::$app->params['exchange_rate']
        ];
        //内陆费用添加
        $inlandModel = new Inland();
        $inland_list = $inlandModel->get_inland_list_by_invoice($invoice_arr);

        foreach ($list as $k => $v) {
            if (isset($incomeList[$v['invoice']])) {
                $list[$k]['income'] = $incomeList[$v['invoice']];
                $list[$k]['date2'] = $incomeList[$v['invoice']]['date'];
            }
        }
        foreach ($list as $k => $v) {

            if (isset($incomeList[$v['invoice']])) {
                $list[$k]['income'] = $incomeList[$v['invoice']];
                $list[$k]['date2'] = $incomeList[$v['invoice']]['date'];
            }

            if (isset($baoxiaoList[$v['invoice']])) {
                $list[$k]['baoxiao'] = $baoxiaoList[$v['invoice']];
            }

            $tmp = 0;
            foreach ($express_list as $v2) {
                if ($v['invoice'] == $v2['invoice_order']) {
                    $tmp += $v2['fee'];
                }
            }

            $list[$k]['express_fee'] = $tmp;

            foreach ($repofee_list as $v2) {
                if ($v['invoice'] == $v2['invoice_order']) {
                    $list[$k]['repo_fee'] = $v2['pay_all'];
                    break;
                }
            }

            foreach ($print_list as $v2) {
                if ($v['invoice'] == $v2['invoice_order']) {
                    $list[$k]['print_fee'] = $v2['fee'];
                    break;
                }
            }

            foreach ($bank_list as $v2) {
                if ($v['invoice'] == $v2['invocie_num']) {
                    $list[$k]['bank'] = $v2['in_money'] - $v2['out_money'];
                    break;
                }
            }

            foreach ($othersList as $v2) {
                if ($v['invoice'] == $v2['invocie_num']) {
                    $list[$k]['others'] = $v2['in_money'] - $v2['out_money'];
                    break;
                }
            }

            foreach ($exchange_list as $v2) {
                $date = isset($v['date2']) ? $v['date2'] : $v['date'];
                if (date('Ym', strtotime($date)) == $v2['month']) {
                    $list[$k]['exchange_rate'] = $v2['rate'];
                    break;
                }
            }

            foreach ($inland_list as $v2) {
                if ($v['invoice'] == $v2['invoice']) {
                    $list[$k]['inland_fee'] = $v2['total_fee'];
                }
            }
        }
        return $list;
    }
}